package com.digua.base.dialog

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.ScreenUtils
import com.digua.base.base.Dialog
import com.digua.base.databinding.DialogSelectPhotoTypeBinding
import com.digua.base.util.PerUtil
import com.digua.base.view.GlideEngine
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener

class SelectPhotoTypeDialog(val resultListener: (ArrayList<LocalMedia>) -> Unit) : Dialog<DialogSelectPhotoTypeBinding>() {
    private lateinit var listener: OnResultCallbackListener<LocalMedia?>
    var maxImgNum = 9

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = super.onCreateView(inflater, container, savedInstanceState)
        dialog?.window?.setGravity(Gravity.BOTTOM)
        return view
    }

    override fun initUI() {
        super.initUI()
        setWinWidth(ScreenUtils.getAppScreenWidth())
        listener = object : OnResultCallbackListener<LocalMedia?> {
            override fun onResult(result: ArrayList<LocalMedia?>) {
                val res = ArrayList<LocalMedia>()
                result.forEach { media -> media?.let { res.add(it) } }
                resultListener(res)
            }

            override fun onCancel() {
            }
        }
        ui.album.setOnClickListener {
            PerUtil.getAlbum {
                PictureSelector.create(this).openGallery(SelectMimeType.ofImage()).setImageEngine(GlideEngine.createGlideEngine()).setSelectionMode(SelectModeConfig.MULTIPLE)
//                    .setSelectorUIStyle(style)
                    .setMaxSelectNum(maxImgNum).forResult(listener)
                dismiss()
            }
        }
        ui.camera.setOnClickListener {
            PerUtil.getCAMERA({
                PictureSelector.create(this).openCamera(SelectMimeType.ofImage()).forResult(listener)
                dismiss()
            })
        }
        ui.cancel.setOnClickListener { dismiss() }
    }
}